package com.hskn.hss.utils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.module.alarmcar.entity.AlarmCar;
import com.hskn.hss.module.alarmcar.mapper.AlarmCarMapper;
import com.hskn.hss.module.kfkcarlocation.entity.KfkCarLocation;
import com.hskn.hss.module.tlcar.entity.TlCar;
import com.hskn.hss.module.tlcar.mapper.TlCarMapper;
import com.hskn.hss.module.tlemployee.mapper.TlEmployeeMapper;
import com.hskn.hss.module.tlequip.entity.TlEquip;
import com.hskn.hss.module.tlequip.mapper.TlEquipMapper;
import com.hskn.hss.module.tlthreshold.entity.TlThreshold;
import com.hskn.hss.module.tlthreshold.mapper.TlThresholdMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class AlarmUtils {

    @Autowired
    TlEquipMapper tlEquipMapper;

    @Autowired
    TlThresholdMapper tlThresholdMapper;


    public Map getCompare(String equcode, String alarmtype) {
        try {
            QueryWrapper<TlEquip> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(TlEquip::getEquipcode, equcode);
            TlEquip tlEquip = tlEquipMapper.selectOne(queryWrapper);
            String objid = tlEquip.getBindobjid();
            List<TlThreshold> threshold = new ArrayList<>();

            QueryWrapper<TlThreshold> thqueryWrapper = new QueryWrapper<>();
            thqueryWrapper.lambda().eq(TlThreshold::getItemid, objid);
            thqueryWrapper.lambda().eq(TlThreshold::getSysname, alarmtype);
            threshold = tlThresholdMapper.selectList(thqueryWrapper);
            Map thvalue = new HashMap();
            if (!threshold.isEmpty()) {//如果根据itemid获取不到 就按type进行获取
                QueryWrapper<TlThreshold> thitemqueryWrapper = new QueryWrapper<>();
                thitemqueryWrapper.lambda().eq(TlThreshold::getFaclitypeid, tlEquip.getBindtype());
                thitemqueryWrapper.lambda().eq(TlThreshold::getSysname, alarmtype);
                threshold = tlThresholdMapper.selectList(thitemqueryWrapper);
                // TODO Expected one result (or null) to be returned by selectOne(), but found: 7
                for (TlThreshold tlThreshold : threshold) {
                    String jsonstr = tlThreshold.getThreshold();
                    thvalue = JSONObject.parseObject(jsonstr, Map.class);
                    thvalue.put("objid", objid);
                    thvalue.put("thresholdid", tlThreshold.getId());
                    thvalue.put("sysname", tlThreshold.getSysname());
                }
            }

            return thvalue;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap();
        }
    }


    /**
     * 车辆报警名称转换
     **/
    public static String changeCarAlarmName(String sysname) {
        switch (sysname) {
            // 车辆-报警项
            case "超速":
                return "carAlarmOverSpeed";
            case "出区域":
                return "carAlarmOutTheArea";
            case "定位失败":
                return "carAlarmLocateFailure";
            case "漏油":
                return "carAlarmSpill";
            case "电源掉电":
                return "carAlarmPowerOff";
            case "电源欠压":
                return "carAlarmPowerSupplyVoltage";
            case "疲劳驾驶":
                return "carAlarmFatigueDriving";
            case "离线":
                return "carAlarmOffline";
            case "进区域":
                return "carAlarmIntoTheArea";
            case "驾驶超时":
                return "carAlarmDrivingTimeout";

    // 这个通用方法有毒。。。。

            case "烟雾报警":
                return "personAlarmSos";
            case "硫化氢报警":
                return "personAlarmLocateFailure";
            case "氨气报警":
                return "personAlarmOffline";
            case "开盖报警":
                return "manholeAlarmOpening";
            case "水位超限":
                return "manholeAlarmWaterLevel";
//            case "巡检提醒":
//                return "manholeAlarmPatrolReminder";
            default:
                return sysname;
        }
    }

    /**
     * 车辆违规名称转换
     **/
    public static String changeCarViolationName(String sysname) {
        switch (sysname) {
            case "作业遗留":
                return "carViolationWorkRemain";
            case "晚到":
                return "carViolationArriveLate";
            case "未到岗":
                return "carViolationNotArrivePost";
            case "水量监测":
                return "carViolationWaterMonitor";
            case "油量监测":
                return "carViolationOilMonitor";
            case "离岗":
                return "carViolationLeavePost";
            case "超时未完成":
                return "carViolationTimeoutUnfinished";
            case "车辆停留":
                return "personViolationStay";
            case "车辆空驶":
                return "carViolationDriveEmpty";
            case "速度限制":
                return "carViolationSpeedLimit";
            case "非法倾倒":
                return "carViolationIllegalDump";
            default:
                return sysname;
        }
    }

    /**
     * 人员报警名称转换
     **/
    public static String changePersonAlarmName(String sysname) {
        switch (sysname) {
            case "SOS求救":
                return "personAlarmSos";
            case "定位失败":
                return "personAlarmLocateFailure";
            case "离线":
                return "personAlarmOffline";
            case "设备低电":
                return "personAlarmLowPower";
            case "进区域":
                return "personAlarmIntoTheArea";
            case "出区域":
                return "personAlarmOutTheArea";
            default:
                return sysname;
        }
    }

    /**
     * 人员违规名称转换
     **/
    public static String changePersonViolationName(String sysname) {
        switch (sysname) {
            case "停留":
                return "personViolationStay";
            case "离岗":
                return "personViolationLeavePost";
            case "聚众":
                return "personViolationTogether";
            default:
                return sysname;
        }
    }

}
